package com.da.javatest.leetCode;

import java.util.Arrays;

/**
 * @author chenlida
 * @date 2019/1/15 11:07
 * @description
 */
public class Code462 {
    public static void main(String[] args) {
        System.out.println(new Code462().minMoves2(
            new int[] {203125577, -349566234, 230332704, 48321315, 66379082, 386516853, 50986744, -250908656,
                -425653504, -212123143}));
    }

    public int minMoves2(int[] nums) {
        if (nums.length < 2) {
            return 0;
        }
        if (nums.length == 2) {
            return Math.abs(nums[0] - nums[1]);
        }
        Arrays.sort(nums);
        int mid = nums.length / 2;
        long t1 = 0, t2 = 0;
        for (int i = 0; i < nums.length; i++) {
            t1 += Math.abs(nums[i] - nums[mid]);
            t2 += Math.abs(nums[i] - nums[mid + 1]);
        }
        return (int)Math.min(t1, t2);
    }
}
